<?php 
class Util
{
	 public static function getLatLang($address)
	    {
			if(isset($address) && $address!= '')
			{
		    	$encodedUrL = 'http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false';
				//echo $encodedUrL ; 
				//echo "<br  />";
				//echo urlencode($encodedUrL);
				//$url = urlencode($encodedUrL);
				//exit;
		    	$geocode = file_get_contents($encodedUrL);
				$output= json_decode($geocode);
				if(isset($output->results) && $output->results != '')
				{    	
					$params['lat'] = $output->results[0]->geometry->location->lat;
					$params['long'] = $output->results[0]->geometry->location->lng;
					return $params;
				}
			}
			
				$params['lat'] = 0;
				$params['long'] = 0;
			
			return $params;
	    }
	    
	  
	public static function getResizeOrScaleImage($source, $destination, $scaling_type = null, $scaling_params=array())
	{
		$image = new SimpleImage();
		$image->load($source);
		switch($scaling_type)
		{
			case 'SCALE' :
				$image->scale($scaling_params['scale']);
			break;
			
			case 'RESIZE' :
				$image->resize($scaling_params['width'],$scaling_params['height']);
			break;
		}
		$image->save($destination);
		
		return isset($destination) ? $destination : null ; 
	}
	
	public static function getCompressionParams($size)
	{
		$scaling_params = array();
		switch($size)
		{
			case ($size > 2621140) : // above 2.5 Mb
				$scaling_params['scale'] = 20; 
			break;
			
			case ($size > 1572864) : // above 1.5 Mb
				$scaling_params['scale'] = 30; 
			break;

			case ($size > 1048576) : // above 1 Mb
				$scaling_params['scale'] = 40; 
			break;
			
			case ($size > 512000) : // above 500 Kb
				$scaling_params['scale'] = 50; 
			break;
			
			case ($size > 307200) : // above 300 Kb
				$scaling_params['scale'] = 60; 
			break;
			
			case ($size > 204800) : // above 200 Kb
				$scaling_params['scale'] = 70; 
			break;
			
			default:
				$scaling_params['scale'] = 100; 
			break;				
		}
		return $scaling_params;
	}
	
	
	public static function truncateString($text, $nbrChar, $append='...') 
	{
     		if(strlen($text) > $nbrChar) {
          		$text = substr($text, 0, $nbrChar);
          		$text .= $append;
     		}
    		return $text;
	}
	
		public static function getCriteriaForArray($tablename, $select=array(), $condition=array(), $params=array(), $operation="AND", $order=array())
	{
		$result = Yii::app()->db->createCommand();
		if($operation == null)
			$operation = "AND"; 		
		if(isset($select) && count($select)>0)
		{
			$result->select(implode(", ", $select));
		}
		else 
		{
			$result->select = "*";
		}	
		
		$result->from($tablename);
		
		if(isset($condition) && isset($params) && count($params) > 0 && count($params) == count($condition))
		{
			$count = count($condition);
			$where = '';
			foreach($condition as $key => $cond)
			{
				$where .= $cond . "=:" . $cond;
				if($count != 1)
					$where .= " ". $operation ." ";

				$params[':'. $cond] = $params[$cond];  
				$count--;
			}
			$result->where($where, $params);
		}
		
		if(isset($order) && isset($order['order_feild']) && isset($order['order']))
		{
			$order_statement = implode(", ", $order['order_feild']);
			$order_statement .= " " . $order['order'];
			$result->order($order_statement);	
		}
		
    	return $result;
    }
	
}